package com.central.player.controller;

import com.central.model.common.PageResult;
import com.central.player.model.*;
import com.central.player.service.PlayerService;
import com.central.player.vo.ConsoleVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 玩家查询控制类
 */
@Slf4j
@RestController
public class PlayerConterller {

    @Autowired
    private PlayerService playerService;

    /**
     * 查询玩家列表基础信息
     * @param params
     * @return
     */
    @PreAuthorize("hasAuthority('select:get/playerBaseInfo')")
    @ApiOperation(value = "用户查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit",value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping("/playerBaseInfo")
    public PageResult<PlayerBase> playerBaseInfo(@RequestParam Map<String, Object> params) {
        Date date = new Date();
        PageResult<PlayerBase> result = playerService.findPlayers(params);
        log.info("玩家基础查询耗时:" + (new Date().getTime() - date.getTime()));
        return result;
    }

    /**
     * 查询某一玩家详细信息
     */
    @PreAuthorize("hasAuthority('select:get/playerBaseInfo')")
    @ApiOperation(value = "玩家详情查询")
    @GetMapping("/playerDetailsInfo/{playerId}")
    public PageResult<PlayerDetailsVO> PlayerDetailedInfo(@PathVariable Long playerId) {
        Date date = new Date();
        if (playerId == null){
            return null;
        }
        PageResult<PlayerDetailsVO> result = playerService.findPlayerDetailed(playerId);
        log.info(result.getData().toString());
        log.info("玩家详情查询耗时:" + (new Date().getTime() - date.getTime()));
        return result;
    }

    /**
     * 查询总的统计信息
     */
    @PreAuthorize("hasAuthority('select:get/playerBaseInfo')")
    @ApiOperation(value = "统计总信息")
    @GetMapping("/totalData")
    public ConsoleVO AllPlayerInfo() {
        ConsoleVO consoleVO = new ConsoleVO();
        List<Guest> listGuest = playerService.listGuest();
        consoleVO.setNotGuest(0);
        consoleVO.setGuest(0);
        listGuest.forEach(guest -> {
            if (guest.getGuest() == 0){
                consoleVO.setNotGuest(guest.getCount());
            }
            if (guest.getGuest() == 1){
                consoleVO.setGuest(guest.getCount());
            }
        });
        consoleVO.setTotalNumber(consoleVO.getGuest()+consoleVO.getNotGuest());
        RegisterAndLogin registerAndLogin = playerService.countLoginToday();
        consoleVO.setLoginCountToday(registerAndLogin.getLoginCountToday());
        consoleVO.setRegisterCountToday(registerAndLogin.getRegisterCountToday());
        consoleVO.setOnlineCountToday(playerService.countOnline());
        CountRechargeDO countRechargeDO = playerService.getRecharge();
        consoleVO.setTotalRechargeMoney(countRechargeDO.getTotalRechargeMoney());
        consoleVO.setTodayRechargeNum(countRechargeDO.getTodayRechargeNum());
        consoleVO.setTodayRechargeMoney(countRechargeDO.getTodayRechargeMoney());
        return consoleVO;
    }
}
